package com.web;

import java.io.IOException;
import util.DateUtil;
import util.IdGenerator;
import java.text.DateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import model.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.xml.ws.RequestWrapper;

import exception.DAOSystemException;

import model.LeaveDTO;
import bizlogic.LeaveManager;

/**
 * Servlet implementation class LeaveEdit
 */
@WebServlet("/LeaveEdit")
public class LeaveEdit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LeaveEdit() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ParseException, DAOSystemException {
			
		
		//TestLeave Update = new TestLeave();
		LeaveManager LM = new LeaveManager();
		LeaveDTO LD = new LeaveDTO();
		LeaveTypeDTO L = new LeaveTypeDTO();
		L.setId(request.getParameter("LeaveType"));
		UserDTO UT = new UserDTO();
		UT.setId(request.getParameter("userID"));
		
//		Date datefrom ; 
//		DateFormat  formatter = new SimpleDateFormat("yyyy-MM-dd");
//		datefrom = (Date)formatter.parse(request.getParameter("from")); 
//		java.sql.Date mysql = (new java.sql.Date(datefrom.getTime()));
//		
//		//pos.setId(IdGenerator.generateHolidayCalendarID().toString());
//		
//		Date dateto;
//		DateFormat  formatter2 = new SimpleDateFormat("yyyy-MM-dd");
//		dateto = (Date)formatter2.parse(request.getParameter("to")); 
//		java.sql.Date mysql2 = (new java.sql.Date(dateto.getTime()));
//		
		LD.setFrom(DateUtil.stringToDATE(request.getParameter("from")));
		LD.setTo(DateUtil.stringToDATE(request.getParameter("to")));
		LD.setStatus("Pending");
		LD.setReason(request.getParameter("reason"));
		LD.setLeaveNature(request.getParameter(""));
		LD.setWorkDissemination("Null");
		LD.setContactDetails(request.getParameter("contactdetail"));
		LD.setLeaveType(L);
		LD.setUser(UT);
		
		
//		Date currentdate = new Date();
//		DateFormat  formatter3 = new SimpleDateFormat("yyyy-MM-dd");
//		Date curdate = (Date)formatter3.(currentdate); 
//		java.sql.Date mysql3 = (new java.sql.Date(dateto.getTime()));
//		LD.setApplyDate((java.sql.Date)currentdate);
		
		//LD.setApplyDate(mysql2);
		
		//LD.setApplyDate(currentdate);
		
	
		LD.setId(request.getParameter("id"));
		
		String ins = (String) request.getParameter("ins");
		Logger.getLogger(getClass().getName()).log(Level.INFO,
				"Insert Flag: " + ins);
		
		if (ins.equalsIgnoreCase("true")) {
			LM.updateLeave(LD);
			//Update.updateLeave(LD);
		} else {
			LM.updateLeave(LD);
		}
		ArrayList<LeaveDTO> data = LM.findAllLeave();
		request.setAttribute("Leave", data);
		RequestDispatcher rd = request.getRequestDispatcher("/leaveeditview");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
